﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Incendios0_1.tools;
using System.Data.Linq;

namespace Incendios0_1.sms
{
    class ContactControler
    {
        public static void AddContact(Contacts newContact)
        {
            DBControler.db.Contacts.InsertOnSubmit(newContact);
        }

        public static bool SaveChanges()
        {
            if (DBControler.SaveChanges() == null)
            {
                return true;
            }
            return false;
        }

        public static Contacts SearchContactByPhone(string phone)
        {
            IQueryable<Contacts> contacts = from u
                                       in DBControler.db.Contacts
                                            where u.Phone == phone
                                            select u;
            if (contacts.Count() > 0)
                return contacts.First();
            else
                return null;
        }

        public static Contacts SearchContactByUser(int userId)
        {
            IQueryable<Contacts> contacts = from u
                                       in DBControler.db.Contacts
                                      where u.UserId == userId
                                      select u;
            if (contacts.Count() > 0)
                return contacts.First();
            else
                return null;
        }

        internal static void Cancel(Contacts Contact)
        {
            DBControler.db.Refresh(RefreshMode.OverwriteCurrentValues, Contact);
        }

        internal static void Delete(Contacts contact)
        {
            DBControler.db.Contacts.DeleteOnSubmit(contact);
            DBControler.db.SubmitChanges();
        }
    }
}
